#include "feeder.h"
#include "bubu.h"
Feeder::Feeder(int attack) : atk(attack)
{
    atk = attack;
    speed = 360.0 * 33 / 800;
}

QRectF Feeder::boundingRect() const
{
    return QRectF(-24, -56, 48, 48);
}

void Feeder::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    Q_UNUSED(option)
    Q_UNUSED(widget)
    painter->drawPixmap(QRect(-24, -56, 48, 48), QPixmap(":/picture/feeder.png"));
}

bool Feeder::collidesWithItem(const QGraphicsItem *other, Qt::ItemSelectionMode mode) const
{
    Q_UNUSED(mode)
    return other->type() == Bubu::Type && qFuzzyCompare(other->y(), y()) && qAbs(other->x() - x()) < 15;
}

void Feeder::advance(int phase)
{
    if (!phase)
        return;
    update();
    QList<QGraphicsItem *> items = collidingItems();
    if (!items.isEmpty())
    {
        Bubu *bubu = qgraphicsitem_cast<Bubu *>(items[rand() % items.size()]);
        bubu->hp -= atk;
        delete this;
        return;
    }
    setX(x() + speed);
    if (x() > 1069)
        delete this;
}
